home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / repnonpr.arc / REPNONPR.PAS
Pascal/Delphi Source File  |  1985-12-11  |  4KB  |  151 lines

  1. {     Replace non-printing characters in Turbo Pascal strings with #???.
  2.  Terminate and restart the string if necessary.
  3.       A file which contains non-printing characters can confuse many printers,
  4.  including the IDS Prism.  Public domain program ALLCHAR.PAS showed me that
  5.  Turbo allows either 255 or 256 characters in a quoted string.  This converter
  6.  let me print out that program.  It could be useful for other programs as
  7.  well.
  8.  
  9.  Lew Paper
  10.  12/10/85}
  11.  
  12.  
  13. PROGRAM repnonpr;
  14.  
  15. CONST
  16.     quote = 39;
  17.  
  18. TYPE
  19.     str255 = STRING[255];
  20.     str80 = STRING[80];
  21.     str3 = STRING[3];
  22.  
  23. VAR
  24.     instr: str255;
  25.     name_in, name_out: str255;
  26.     infile, outfile: TEXT;
  27.     good_name: INTEGER;
  28.     answer: CHAR;
  29.     i: INTEGER;
  30.     quoted: BOOLEAN;
  31.     char_str: str3;
  32.  
  33. BEGIN {PROGRAM repnonpr}
  34.  
  35.     CLRSCR;
  36.     WRITELN('Replace nonprinting characters in strings with #...');
  37.     WRITELN;
  38.  
  39.     {Get input file}
  40.     REPEAT {UNTIL good_name = 0;}
  41.         GOTOXY(1, 3);
  42.         WRITE('Input file: ');
  43.         READLN(name_in);
  44.         CLREOL;
  45.         ASSIGN(infile, name_in);
  46.         {$I-}
  47.         RESET(infile);
  48.         {$I+}
  49.         good_name := IORESULT;
  50.         IF good_name > 0
  51.         THEN
  52.             WRITE('Unable to open file ', name_in, '. Enter new name');
  53.     UNTIL good_name = 0;
  54.  
  55.     {Get output file}
  56.     REPEAT {UNTIL good_name = 0;}
  57.         GOTOXY(1, 5);
  58.         WRITE('Output file: ');
  59.         READLN(name_out);
  60.         CLREOL;
  61.         ASSIGN(outfile, name_out);
  62.         {$I-}
  63.         RESET(outfile);
  64.         {$I+}
  65.         IF IORESULT = 0
  66.         THEN
  67.             BEGIN
  68.             CLOSE(outfile);
  69.             REPEAT {UNTIL answer IN ['Y', 'N']}
  70.                 GOTOXY(1, 6);
  71.                 WRITE('Overwrite file ', name_out, ' (Y or N)? ');
  72.                 READLN(answer);
  73.                 CLREOL;
  74.                 answer := UPCASE(answer);
  75.                 CASE answer OF
  76.                 'Y':
  77.                     good_name := 0;
  78.  
  79.                 'N':
  80.                     good_name := 1;
  81.                 ELSE
  82.                     WRITE('"Y" or "N" only please');
  83.                 END; { CASE answer }
  84.             IF answer IN ['Y', 'N']
  85.             THEN
  86.                 BEGIN
  87.                 GOTOXY(1,6);
  88.                 CLREOL;
  89.                 END; {IF answer IN ['Y', 'N']}
  90.             UNTIL answer IN ['Y', 'N'];
  91.             END {IF IORESULT = 0}
  92.         ELSE
  93.             good_name := 0;
  94.         IF good_name = 0
  95.         THEN
  96.             BEGIN
  97.             {$I-}
  98.             REWRITE(outfile);
  99.             {$I+}
  100.             good_name := IORESULT;
  101.             IF good_name > 0
  102.             THEN
  103.                 WRITE('Unable to open file ', name_in, '. Enter new name');
  104.             END; {IF good_name = 0}
  105.     UNTIL good_name = 0;
  106.  
  107.     {Process file}
  108.     WHILE NOT EOF(infile)
  109.     DO
  110.         BEGIN
  111.         READLN(infile, instr);
  112.         quoted := FALSE;
  113.         i := 0;
  114.         WHILE i < LENGTH(instr)
  115.         DO
  116.             BEGIN
  117.             i := SUCC(i);
  118.             IF quoted
  119.             THEN
  120.                 IF (ORD(instr[i]) < 32) OR (ORD(instr[i]) > 126)
  121.                 THEN
  122.                     BEGIN
  123.                     STR(ORD(instr[i]), char_str);
  124.                     IF ORD(instr[i + 1]) = quote
  125.                     THEN
  126.                         DELETE(instr, i + 1, 1)    {Remove trailing quote}
  127.                     ELSE
  128.                         INSERT(CHR(quote), instr, i + 1); {End split}
  129.                     INSERT('#' + char_str, instr, i + 1); {Printing character}
  130.                     DELETE(instr, i, 1); {Remove character}
  131.                     IF ORD(instr[i - 1]) = quote
  132.                     THEN
  133.                         BEGIN
  134.                         DELETE(instr, i - 1, 1);    {Remove leading quote}
  135.                         quoted := FALSE;
  136.                         END {ORD(instr[i - 1]) = quote} {Start split}
  137.                     ELSE
  138.                         INSERT(CHR(quote), instr, i);
  139.                     END; {IF (ORD(instr[i]) < 32) OR (ORD(instr[i]) > 126)}
  140.             IF ORD(instr[i]) = quote
  141.             THEN
  142.                 quoted := NOT quoted;
  143.             END; {WHILE i < LENGTH(instr)}
  144.         WRITELN(outfile, instr);
  145.         END; {WHILE NOT EOF(infile)}
  146.  
  147.     CLOSE(infile);
  148.     CLOSE(outfile);
  149.  
  150. END. {PROGRAM repnonpr}
  151.